<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Base class for list. (BaseList)
 * 
 * Widgets for displaying a list sytle or same.
 * Includes a list widget and its specific item widget.
 * 
 * 用来显示一个列表的控件.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Adds the 'optionswitch' variable.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_widget.php';

// Definitions.
// After it, no item would display; Might be LARGER than the 'XXX_PERPAGE'
define('LIST_MAX_ITEM_COUNT',		100);


/**
 * Widget for displaying List. Uh, list is reserved name!
 */
class BaseList extends Widget
{
	/**
	 * Item object that constructed the list content.
	 */
	var $obj = null;
	
	/**
	 * Constructor.
	 */
	function __construct(&$obj, $out = null)
	{
		parent::__construct($out);
		$this->obj =& $obj;
	}
	
	/**
	 * Starts a list.
	 * The notice or some sayings of the list. Might some labels.
	 * Like '_ list.'!
	 * 
	 * Might be overrided.!
	 */
	function showStart()
	{
		$this->out->elementStart('dl', array('class' => 'cls_dl_list'));
		
		// The List title.
		$this->out->element('dt', null, '列表');
	}
	
	/**
	 * Ends a list.
	 */
	function showEnd()
	{
		$this->out->elementEnd('dl');
	}
	
	/**
	 * Show the list
	 * 
	 * @return int, count of item listed.
	 */
	function show()
	{
		$this->showStart();
		
		$itemcnt = 0;
		while ($this->obj->fetch() && $itemcnt <= LIST_MAX_ITEM_COUNT) {
			++$itemcnt;
			if ($itemcnt > LIST_MAX_ITEM_COUNT) {
				break;
			}
			
			try {
				$item = $this->newListItem($this->obj);
				$item->show();
			} catch (Exception $e) {
				// FIXME: Log?
				continue;
			}
		}
		
		$this->showEnd();
		
		return $itemcnt;
	}
	
	/**
	 * Returns a new list item for the current objects.
	 */
	function newListItem(&$obj)
	{
		return new BaseListItem($obj, $this->out);
	}
}

/**
 * Widget for displaying a single item with some options.
 */
class BaseListItem extends Widget
{
	/**
	 * The item this item will show().
	 */
	var $obj = null;
	
	/**
	 * Options switchs.
	 */
	var $optionswitch = false;
	
	/**
	 * Constructor.
	 */
	function __construct($obj, $out = null)
	{
		parent::__construct($out);
		
		$this->obj =& $obj;
	}
	
	/**
	 * Recipe function for displaying a single item.
	 */
	function show()
	{
		if (empty($this->obj)) {
			return;
		}
		
		$this->prepareData();
		$this->showStart();
		$this->showDataInfo();
		if (true == $this->optionswitch) {
			$this->showOptions();
		}
		$this->showEnd();
	}
	
	/**
	 * Do some data preparation.
	 */
	function prepareData()
	{
	}
	
	/**
	 * Start a single item.
	 * Overriding.
	 */
	function showStart()
	{
		$this->out->elementStart('dd', array('class' => 'cls_dd_listitem'));
		
		$this->out->elementStart('fieldset');
		$legend = 'id: ' . $this->obj->id . 
				  '; status: ' . $this->obj->status;
		$this->out->element('legend', null, $legend);
	}
	
	/**
	 * Finish the note news item.
	 */
	function showEnd()
	{
		$this->out->elementEnd('fieldset');
		$this->out->elementEnd('dd');
	}
	
	/**
	 * Overriding. Example:
	 */
	function showDataInfo()
	{
		/*
		$this->out->elementStart('ul');
			$this->out->elementStart('li');
			$this->out->elementStart('div', array('class' => 'cls_div_message'));
			$this->out->text('新闻概要: ');
			$this->out->raw('<span class="cls_span_message">' .
						    $this->subnews->message . 
						    '</span>');
			$this->out->elementEnd('div');
			$this->out->elementEnd('li');
			
			$this->out->elementStart('li');
			$this->out->raw('<span class="cls_span_readmore">' .
							'<a target="_blank" href="' .
							slh_action2url('note', array('gnews_id' => $this->subnews->news_id)) .
							'">' . 
							'浏览详细内容...' .
							'</a>' . 
							'</span>');
			$this->out->elementEnd('li');
			
			$this->out->elementStart('li', array('class' => 'cls_li_author'));
			$this->out->text('作者: ');
			$this->out->raw('<span class="cls_span_author">' .
							//FIXME: Get the author by the user_id
					   		$this->subnews->user_id . 
					   		'</span>');
			$this->out->elementEnd('li');
			
			$this->out->elementStart('li');
			$this->out->text('发布时间: ');
			$this->out->raw('<span class="cls_span_created">' .
						    $this->subnews->created . 
						    '</span>');
			$this->out->elementEnd('li');
		$this->out->elementEnd('ul');
		*/
	}
	
	/**
	 * Overriding. Example:
	 */
	function showOptions()
	{
		/*
		$this->out->elementStart('div', 'cls_div_newsoptions');
		
// TODO: Check if has rights to delete or more options.
		$this->out->elementStart('ul', array('class' => 'cls_ul_inline cls_a_newsoption'));
			if (true) {
			$this->out->elementStart('li');
			$this->out->element('a', array('href' => slh_action2url('deletenotenews',
								  	   							  	array('gnews_id' => $this->subnews->news_id)),
										   'title'=> '把这条新鲜事(新闻或者活动)删掉哈!'),
								'删除');
			$this->out->elementEnd('li');
			}
			
			if (false) {
			$this->out->elementStart('li');
			$this->out->elementEnd('li');
			}
		$this->out->elementEnd('ul');
		
		$this->out->elementEnd('div');
	*/
	}
}

?>